var User = require('../model/user')
var Shop = require('../model/shop')
var ErrorCode = require('./errorCode')

var shopApi = {
    createShop: function (req, res) {
        var icon = req.files.icon.path
        var shop = {
            name: req.body.name,
            icon: icon,
            addr: req.body.addr,
            school: req.body.school,
            notice: [],
            user: req.userid,
            info: req.body.info,
            open: false
        }
        Shop.create(shop, function (err, s) {
            if (err) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                res.json(s)
                return
            }
        })

    },
    updateShop: function (req, res) {
        var iconE = req.files.icon
        var shopid = req.body.id
        var name = req.body.name
        var icon
        if(iconE){
            icon = iconE.path
        }
        var addr = req.body.addr
        var school = req.body.school
        var info = req.body.info
        Shop.findOne({_id:shopid}, function (err, s) {
            if (!s) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                if(icon){
                    s.icon = icon
                }
                s.school = school
                s.addr = addr
                s.name = name
                s.info = info
                s.save(function(err){
                    if(err){
                        res.json(400, ErrorCode.CommonError)
                        return
                    }else{
                        res.json(s)
                        return
                    }
                }) 
            }
        })

    },
    listShopByAddr: function (req, res) {
        var addr = req.query.addr
        Shop.find({ addr: addr }, function (err, shop) {
            res.json(shop)
            return
        })
    }
    ,
    myShop: function (req, res) {
        Shop.find({ user: req.userid }, function (err, shop) {
            res.json(shop)
            return
        })
    }
    ,
    publicNotice: function (req, res) {
        var notice = req.body.notice
        var shopid = req.body.id
        Shop.findOne({ _id: shopid }, function (err, shop) {
            shop.notice.push(notice)
            shop.save(function (err) {
                res.json(shop)
                return
            })
        })

    },
    openShop: function (req, res) {
        var shopid = req.query.id
        Shop.findOne({ _id: shopid }, function (err, shop) {
            if (err) {
                res.json(400, err)
                return
            }
            shop.open = true
            shop.save(function (err) {
                res.json(shop)
                return
            })
        })
    },
    closeShop: function (req, res) {
        var shopid = req.query.id
        Shop.findOne({ _id: shopid }, function (err, shop) {
            if (err) {
                res.json(400, err)
                return
            }
            shop.open = false
            shop.save(function (err) {
                res.json(shop)
                return
            })
        })
    }



}
module.exports = shopApi